package cbbx_sm.decision_maker.search;

import java.util.Hashtable;

public class Event {
	//private boolean approximate = true;
	//private Boolean floor = true;
	public static double p1 = 1;
	public static double p0 = 0;
	private double[] possibleValues = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1};
	private Hashtable<Double, Integer> ordinalHash;
	//public static double[] possibleValues = {0, 0.25, 0.5, 0.75, 1};
	
	public Event(double[] possibleValues) {
		this.possibleValues = possibleValues;
		//this.approximate = approximate;
		//this.floor = floor;
	}
	public double[] values(){
		return possibleValues;
	}

	public int ordinal(double d) {
		if (ordinalHash == null){
			ordinalHash = new Hashtable<Double, Integer>();
			
			for (int i=0; i<possibleValues.length; i++){
				ordinalHash.put(possibleValues[i], i);
			}
		}
		return ordinalHash.get(d);
	}
	
	/**
	 * Approximates the probability value based on the approximation level being used.
	 * @param p the probability in range [0..1].
	 * @param floor true if the floor of the probability is needed, otherwise ceiling is returned.
	 * 
	 * @return the event closest.
	 */
	public double approximateEventForProbability(double p, boolean floor) {		
		int start = 0;
		int end = possibleValues.length; 
		int i = (start+end)/2;
		
		// This loop returns maximal value that is smaller or equal to p (floor) 
		while (!(possibleValues[i]<=p && (i+1==end || possibleValues[i+1]>p) ) ){
			if (p<possibleValues[i]){
				end = i;
			} else {
				start = i;
			}
			i = (start+end)/2;
		}
		

		
		if (floor || possibleValues[i] == p){
			return possibleValues[i];
		}
		// The is the returned value in case of ceiling.
		return possibleValues[i+1];
	}
}
